package com.dream.cms.dao;

import com.dream.cms.entity.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class LogRepositoryDymi {

    @PersistenceContext
    private EntityManager entityManager;

    @Modifying
    @Transactional
    public int insertLog(List<Log> logs){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ");
        sb.append(getTableName());
        sb.append(" (body_bytes_sent,client_real_ip,host_name,http_referer,http_user_agent,remote_addr,remote_user,request,request_time,status,timestamp,upstream_addr,upstream_response_length,upstream_response_time,upstream_status,nginx_ip,content) values");
        if(!CollectionUtils.isEmpty(logs)){
            for(int i =0,len = logs.size();i<len;i++){
                Log log = logs.get(i);
                sb.append("(");
                sb.append("'"+log.getBodyBytesSent()+"',");
                sb.append("'"+log.getClientRealIp()+"',");
                sb.append("'"+log.getHostName()+"',");
                sb.append("'"+log.getHttpReferer()+"',");
                sb.append("'"+log.getHttpUserAgent()+"',");
                sb.append("'"+log.getRemoteAddr()+"',");
                sb.append("'"+log.getRemoteUser()+"',");
                sb.append("'"+log.getRequest()+"',");
                sb.append("'"+log.getRequestTime()+"',");
                sb.append("'"+log.getStatus()+"',");
                sb.append("'"+log.getRequestTime()+"',");
                sb.append("'"+log.getUpstreamAddr()+"',");
                sb.append("'"+log.getUpstreamResponseLength()+"',");
                sb.append("'"+log.getUpstreamResponseTime()+"',");
                sb.append("'"+log.getUpstream_status()+"',");
                sb.append("'"+log.getNginxIp()+"'");
                sb.append("'"+log.getContent()+"'");
                sb.append(")");
                if(i == (len-1)){
                    sb.append(";");
                }else{
                    sb.append(",");
                }
            }
        }
        int rows = 0;
        try {
            Query query = entityManager.createNativeQuery(sb.toString());
            rows = query.executeUpdate();
        } catch (Exception e){
            log.debug("wgq sql is:"+sb.toString());
            log.error(e.getMessage());
        }
        return rows;
    }

    private static String getTableName(){
        String table = DateFormatUtils.format(new Date(),"yyyyMMdd");
        return "tb_log_" + table;
    }

    public static void main(String[] args){
        System.out.println(getTableName());
    }


}
